﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace TesteOnline
{
    public partial class TestWebForm : System.Web.UI.Page
    {
        quizdb.QuizDb quiz = new quizdb.QuizDb(new MySqlConnection("Persist Security Info=False;server=localhost;database=quizdb;uid=root;server=localhost;database=quizdb;uid=root;pwd='';initial catalog=quizdb"));
        int valor;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                valor=1;
                DropDownList1.DataSource = quiz.MaterIA;
                DropDownList1.DataTextField = "Nome";
                DropDownList1.DataValueField = "IdMateria";


                //DropDownList1.Items.Insert(0, new ListItem(""));

                DataBind();
            }
            else
            {
                valor+=2;
            }

            Response.Write("valor: " + valor + "</br>");
            
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            valor += 2;
            int materiaId = Int32.Parse(DropDownList1.Items[DropDownList1.SelectedIndex].Value);
            var v = from h in quiz.TopicO
                    where h.MateriaIDMateria == materiaId
                    select h;
            Response.Write(materiaId);
            Response.Write(v.Count());

            if (v.Count() == 0)
            {
                DropDownList2.Items.Clear();
            }
            else
            {
                DropDownList2.DataSource = quiz.TopicO;
                DropDownList2.DataTextField = "Nome";
                DropDownList2.DataValueField = "idTopico";
            }
            DataBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int materiaId = Int32.Parse(DropDownList2.Items[DropDownList2.SelectedIndex].Value);
            var v = from h in quiz.SubtopicO
                    where h.TopicO.IDTopico == materiaId
                    
                    select h;
            Response.Write(materiaId);
            Response.Write(v.Count());

            if (v.Count() == 0)
            {
                DropDownList3.Items.Clear();
            }
            else
            {
                DropDownList3.DataSource = quiz.SubtopicO;
                DropDownList3.DataTextField = "Nome";
                DropDownList3.DataValueField = "IDSubTopico";
            }
            DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            quizdb.QuestAO q = new quizdb.QuestAO();
            q.DataCriacao = DateTime.Now;
            q.Gabarito = 1;
            q.MateriaIDMateria = Int32.Parse( DropDownList1.SelectedItem.Value );
            q.Texto = TextBox3.Text;
            q.Tipo = 1;
           

            quiz.QuestAO.InsertOnSubmit(q);
            quiz.SubmitChanges();
            //q.Gabarito = 1;
            //q.MaterIA = quiz.MaterIA.Fin
             
        }




    }
}